package com.huika.dealontop.control.myinfo.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.huika.dealontop.R;
import com.huika.dealontop.control.base.BaseAda;
import com.huika.dealontop.control.timelimit.fragment.ShoppingCartFragment;
import com.huika.dealontop.support.domain.SoppingCartBean;
import com.huika.dealontop.utils.ImageLoaderHelper;
import com.huika.huikalib.utils.StringUtils;

/**
 * Created by yangyuan on 2016/1/11.
 */
public class ShoppingCartAdapter extends BaseAda<SoppingCartBean> {

    ShoppingCartFragment shoppingcarAct;

    public  void setAct(ShoppingCartFragment shoppingcarAct){
        this.shoppingcarAct = shoppingcarAct;
    }

    public ShoppingCartAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_list_app, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        SoppingCartBean item = getItem(position);

        ImageLoaderHelper.displayImage(item.Pic, holder.shopping_cart_pic, R.mipmap.morenpic);
        if (StringUtils.isEmpty(item.SkuName)) {
            holder.shopping_cart_title.setText(item.ProductName);
        }else{
            holder.shopping_cart_title.setText(item.SkuName + item.ProductName);
        }
        holder.shopping_cart_number.setText(item.Quantity + "");
        holder.shopping_cart_total_prices.setText("฿" + (int)item.HKPrice);

        if (item.Status == 5) {//已下架
            holder.shopping_cart_soldout.setVisibility(View.VISIBLE);
            holder.shopping_cart_check.setImageResource(R.mipmap.common_unselect_choise_icon);
            holder.shopping_cart_pic.setAlpha(0.6f);
            holder.shopping_cart_title.setTextColor(mContext.getResources().getColor(R.color.color_b1b1b1));
            holder.shopping_cart_number.setTextColor(mContext.getResources().getColor(R.color.color_b1b1b1));
            holder.shopping_cart_total_prices.setTextColor(mContext.getResources().getColor(R.color.color_b1b1b1));
        }else{
            holder.shopping_cart_soldout.setVisibility(View.GONE);
            holder.shopping_cart_pic.setAlpha(1.0f);
            holder.shopping_cart_title.setTextColor(mContext.getResources().getColor(R.color.color_333333));
            holder.shopping_cart_number.setTextColor(mContext.getResources().getColor(R.color.color_333333));
            holder.shopping_cart_total_prices.setTextColor(mContext.getResources().getColor(R.color.color_333333));
            if (item.IsSelected == 1) {
                holder.shopping_cart_check.setImageResource(R.mipmap.common_select_choise_icon);
            }else{
                holder.shopping_cart_check.setImageResource(R.mipmap.common_unselect_choise_icon);
            }
        }

        if (item.Status == 5 || item.Quantity <= 1) {
            holder.shopping_cart_subtract.setBackgroundResource(R.mipmap.bt_add_01);
        }else{
            holder.shopping_cart_subtract.setBackgroundResource(R.mipmap.bt_add);
        }
        if (item.Status == 5 || item.Quantity >= 50) {
            holder.shopping_cart_add.setBackgroundResource(R.mipmap.bt_plus_01);
        }else{
            holder.shopping_cart_add.setBackgroundResource(R.mipmap.bt_plus);
        }

        setOnclick(holder.shopping_cart_check,item);
//        setOnclick(holder.shopping_cart_title,item);
        setOnclick(holder.shopping_cart_subtract,item);
        setOnclick(holder.shopping_cart_number,item);
        setOnclick(holder.shopping_cart_add,item);

        return convertView;
    }

    class ViewHolder {
        ImageView shopping_cart_check;
        TextView shopping_cart_soldout;
        ImageView shopping_cart_pic;
        TextView shopping_cart_title;
        TextView shopping_cart_subtract;
        TextView shopping_cart_number;
        TextView shopping_cart_add;
        TextView shopping_cart_total_prices;

        public ViewHolder(View view) {
            shopping_cart_check = (ImageView) view.findViewById(R.id.shopping_cart_check);
            shopping_cart_soldout = (TextView) view.findViewById(R.id.shopping_cart_soldout);
            shopping_cart_pic = (ImageView) view.findViewById(R.id.shopping_cart_pic);
            shopping_cart_title = (TextView) view.findViewById(R.id.shopping_cart_title);
            shopping_cart_subtract = (TextView) view.findViewById(R.id.shopping_cart_subtract);
            shopping_cart_number = (TextView) view.findViewById(R.id.shopping_cart_number);
            shopping_cart_add = (TextView) view.findViewById(R.id.shopping_cart_add);
            shopping_cart_total_prices = (TextView) view.findViewById(R.id.shopping_cart_total_prices);
        }
    }

    private void setOnclick(View view,SoppingCartBean item){
        view.setOnClickListener(new MyOnClickListener(item));
    }

    //点击监听事件
    class MyOnClickListener implements View.OnClickListener {

        private SoppingCartBean item;

        public MyOnClickListener(SoppingCartBean item) {
            this.item = item;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.shopping_cart_check://是否勾选
                    if (item.Status == 5) {
                        return;
                    }
                    if (item.IsSelected == 1 ) {
                        double itemnum = item.HKPrice * item.Quantity;
                        if ((shoppingcarAct.tempCoup > 0 || shoppingcarAct.couponDiscountPrice > 0) && (shoppingcarAct.priea - itemnum) < 99) {
                            shoppingcarAct.GetViewDialog(1,"1",false , item.ShoppingCartId+"",0,0);
                        }else if ((shoppingcarAct.tempCoup > 0 ) && (shoppingcarAct.priea - itemnum) <= shoppingcarAct.tempCoup){
                            shoppingcarAct.GetViewDialog(2,"1",false , item.ShoppingCartId+"",0,0);
                        }else{
                            shoppingcarAct.getSelectGoods(false , item.ShoppingCartId+"");
                        }
                    }else{
                        shoppingcarAct.getSelectGoods(true , item.ShoppingCartId+"");
                    }
                    break;
                case R.id.shopping_cart_title://进入详情
                    break;
                case R.id.shopping_cart_subtract://数量减
                    if (item.Quantity > 0 && item.Status != 5) {
                        double itemnum = item.HKPrice;

                        if ((shoppingcarAct.tempCoup > 0 || shoppingcarAct.couponDiscountPrice > 0) && (shoppingcarAct.priea - itemnum) < 99) {
                            shoppingcarAct.GetViewDialog(1,"2",false , "",(item.Quantity-1),item.SkuProductId);
                        }else if ((shoppingcarAct.tempCoup > 0 ) && (shoppingcarAct.priea - itemnum) <= shoppingcarAct.tempCoup){
                            shoppingcarAct.GetViewDialog(2,"2",false , "",(item.Quantity-1),item.SkuProductId);
                        }else{
                            shoppingcarAct.getNumberData((item.Quantity - 1), item.SkuProductId);
                        }
                    }
                    break;
                case R.id.shopping_cart_number:
//                    shoppingcarAct.showToastMsg("数量选择");
                    break;
                case R.id.shopping_cart_add://数量加
//                    if (item.Quantity < 50 && item.Status != 5) {
                    if (item.Status != 5) {
                        shoppingcarAct.getNumberData((item.Quantity+1) , item.SkuProductId);
                    }
                    break;
            }
        }
    }

}